/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controller.actions;

import controler.formbeans.AboutUsBean;
import controler.formbeans.LoginBean;
import java.util.Vector;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import model.logic.db.AdminFunctionality;
import model.logic.db.CommonWSFunctionality;
import model.logic.db.UserFunctions;
import model.pojo.AboutUs;
import model.pojo.Admins;
import model.pojo.Applicants;
import model.pojo.Users;
import model.pojo.Vacancies;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

/**
 *
 * @author Maha
 */
public class RedirectAction extends DispatchAction {
    
    public ActionForward goToContactUsPage(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        CommonWSFunctionality ws = new CommonWSFunctionality();
        Vector<AboutUs> aboutUs = ws.getAboutUsMenuAndContents();
        ((AboutUsBean) form).setAboutUs(aboutUs);
        return mapping.findForward("contactus");
    }
    
    public ActionForward goToHomePage(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        CommonWSFunctionality ws = new CommonWSFunctionality();
        Vector<AboutUs> aboutUsMenuAndContents = ws.getAboutUsMenuAndContents();
        ((AboutUsBean) form).setAboutUs(aboutUsMenuAndContents);
        HttpSession session = request.getSession(false);
        if ((Admins) session.getAttribute("admin") != null) {
            Admins admin = (Admins) session.getAttribute("admin");
            AdminFunctionality adminfunc = new AdminFunctionality();
            ((AboutUsBean) form).setPriv(adminfunc.getAdminPrivilidges(admin.getAdminId()));
        }
        ((AboutUsBean) form).setServices(ws.getServicesAndContents());
        ((AboutUsBean) form).setProducts(ws.getProductsAndContents());
        ((AboutUsBean) form).setNews(ws.getNews());
        ((AboutUsBean) form).setVacancies(ws.getVacancies());
        return mapping.findForward("home");
    }
    
    public ActionForward logout(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        HttpSession session = request.getSession(false);
        
        session.invalidate();
        
        
        return mapping.findForward("load");
    }
    
    public ActionForward vacancies(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        CommonWSFunctionality cwf = new CommonWSFunctionality();
        Vector<Vacancies> vacancies = cwf.getVacancies();
        
        ((AboutUsBean) form).setVacancies(vacancies);
        return mapping.findForward("vac");
    }
    
    public ActionForward goToApplicant(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        ActionForward forward = new ActionForward();
        CommonWSFunctionality ws = new CommonWSFunctionality();
        Vector<AboutUs> aboutUs = ws.getAboutUsMenuAndContents();
        ((AboutUsBean) form).setAboutUs(aboutUs);
        UserFunctions uf = new UserFunctions();
        ((AboutUsBean) form).setCountry(uf.getCountries());
        ((AboutUsBean) form).setElevel(uf.getEduLevels());
        ((AboutUsBean) form).setInst(uf.getInst());
        ((AboutUsBean) form).setGrades(uf.getGrades());
        ((AboutUsBean) form).setStudy(uf.getStudyFields());
        // ((AboutUsBean) form).setCareer(uf.getCareers());
        ((AboutUsBean) form).setVacancies(uf.getVacncies());
        
        HttpSession session = request.getSession();
        Users user = (Users) session.getAttribute("user");
        Applicants applicant = uf.getApplicant(user.getUserId());
        if (applicant != null) {
            forward = mapping.findForward("op");
            ((AboutUsBean) form).setMessage("Sorry, You seemed to be Applied Before, you can update your data");
        } else {
            forward = mapping.findForward("app");
        }
        return forward;
    }
}
